﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.ObjectModel;
using System.Collections;
using Eassets.Service.Secure;
using Eassets.Layered.Domain.Secure;
using Eassets.Layered.Domain.Global;

namespace Eassets.UI.Secure
{
    public partial class SecurityGroupForm : System.Web.UI.Page
    {
        private Boolean checkSaveModify;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                GetSecurityGroups();
                ViewState["ButtonBehaviour"] = "Load";
                DisableComponants();

            }
        }



        private void GetSecurityGroups()
        {
            SecurityGroupService securityGroupService = new SecurityGroupService();
            SecurityGroup securityGroup = new SecurityGroup();

            try
            {

                Collection<SecurityGroup> SecurityGroups = new Collection<SecurityGroup>();
                SecurityGroups = securityGroupService.GetAllSecurityGroups();

                if (SecurityGroups.Count > 0)
                {
                    gridViewSecurityGroupList.DataSource = securityGroupService.GetAllSecurityGroups();
                }

                gridViewSecurityGroupList.DataBind();

            }
            catch (Exception exception)
            {
                throw;
            }
        }

        private Boolean CheckMandatory(string addOrModify)
        {
            if (addOrModify == "A")
            {
                if (textBoxSecurityGroupCode.Text == String.Empty)
                {
                    FlashMessage1.Display("[Security Group Code] cannot be blank", "errormessage");
                    return false;
                }
                else if (textBoxDescription.Text == String.Empty)
                {
                    FlashMessage1.Display("[Description] cannot be blank", "errormessage");
                    return false;
                }
            }

            return true;
        }

        protected void ButtonSave_Click(object sender, EventArgs e)
        {

            if (CheckMandatory("A"))
            {

                SecurityGroupService securityGroupService = new SecurityGroupService();
                SecurityGroup securityGroup = new SecurityGroup();
                OrganizationLevelList organizationLevelList = new OrganizationLevelList();

                organizationLevelList.OrganizationLevelListId = Convert.ToInt64(Session[Global.KeyCurrentCompanyId]);

                securityGroup.Code = textBoxSecurityGroupCode.Text;
                securityGroup.Description = textBoxDescription.Text;
                securityGroup.OrganizationLevelList = organizationLevelList;

                try
                {
                    if (Page.IsValid)
                    {
                        securityGroupService.AddSecurityGroup(securityGroup);
                        FlashMessage1.Message = "Successfully Saved";
                        FlashMessage1.CssClass = "okmessage";
                        FlashMessage1.Display();
                    }
                }
                catch (Exception exception)
                {
                    FlashMessage1.Message = "Saving Failed";
                    FlashMessage1.CssClass = "errormessage";
                    FlashMessage1.Display();
                }

                GetSecurityGroups();
                ClearInputs();

            }
        }

        protected void gridViewSecurityGroupList_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = gridViewSecurityGroupList.SelectedRow;

            hiddenFieldSecurityGroup.Value = row.Cells[1].Text;
            textBoxSecurityGroupCode.Text = row.Cells[2].Text;
            textBoxDescription.Text = row.Cells[3].Text;
            ViewState["ButtonBehaviour"] = "GridSelectedChanged";
            DisableComponants();

        }

        protected void gridViewSecurityGroupList_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells.Count > 1)
            {
                e.Row.Cells[1].Visible = false;
            }
        }

        protected void buttonSaveEdit_Click(object sender, EventArgs e)
        {

            if (CheckMandatory("M"))
            {
                if (hiddenFieldSecurityGroup.Value != String.Empty)
                {
                    SecurityGroupService securityGroupService = new SecurityGroupService();
                    SecurityGroup securityGroup = new SecurityGroup();
                    OrganizationLevelList organizationLevelList = new OrganizationLevelList();

                    organizationLevelList.OrganizationLevelListId = Convert.ToInt64(Session[Global.KeyCurrentCompanyId]);

                    securityGroup.SecurityGroupId = Convert.ToInt64(hiddenFieldSecurityGroup.Value);
                    securityGroup.Code = textBoxSecurityGroupCode.Text;
                    securityGroup.Description = textBoxDescription.Text;
                    securityGroup.OrganizationLevelList = organizationLevelList;

                    try
                    {
                        if (Page.IsValid)
                        {
                            securityGroupService.ModifySecurityGroup(securityGroup);
                            FlashMessage1.Message = "Successfully Saved";
                            FlashMessage1.CssClass = "okmessage";
                            FlashMessage1.Display();
                        }
                    }
                    catch (Exception exception)
                    {
                        FlashMessage1.Message = "Saving Failed";
                        FlashMessage1.CssClass = "errormessage";
                        FlashMessage1.Display();
                    }

                    GetSecurityGroups();
                    ClearInputs();
                }
            }
        }

        protected void buttonCancelAdd_Click(object sender, EventArgs e)
        {
            ClearInputs();
        }

        protected void buttonDeleteEdit_Click(object sender, EventArgs e)
        {
            if (hiddenFieldSecurityGroup.Value != String.Empty)
            {
                SecurityGroupService securityGroupService = new SecurityGroupService();

                try
                {
                    if (Page.IsValid)
                    {
                        securityGroupService.DeleteSecurityGroup(Convert.ToInt64(hiddenFieldSecurityGroup.Value));
                        FlashMessage1.Message = "Successfully Saved";
                        FlashMessage1.CssClass = "okmessage";
                        FlashMessage1.Display();
                    }
                }
                catch (Exception exception)
                {
                    FlashMessage1.Message = "Saving Failed";
                    FlashMessage1.CssClass = "errormessage";
                    FlashMessage1.Display();
                }

                GetSecurityGroups();
                ClearInputs();
            }
        }

        protected void buttonCancelEdit_Click(object sender, EventArgs e)
        {
            ClearInputs();
        }

        protected void ButtonClear_Click(object sender, EventArgs e) 
        {
            ClearInputs();
        }

        private void ClearInputs()
        {
            textBoxSecurityGroupCode.Text = String.Empty;
            textBoxDescription.Text = String.Empty;
            hiddenFieldSecurityGroup.Value = String.Empty;


        }

        protected void ButtonAdd_Click(object sender, EventArgs e)
        {
            ViewState["ButtonBehaviour"] = "Add";
            DisableComponants();
        }

        protected void ButtonModify_Click(object sender, EventArgs e)
        {

            checkSaveModify = false;
            ViewState["ButtonBehaviour"] = "Modify";
            DisableComponants();
        }

        protected void ButtonDelete_Click(object sender, EventArgs e)
        {
            if (hiddenFieldSecurityGroup.Value == String.Empty)
            {
                FlashMessage1.Display("No record is selected", "errormessage");
            }
            else
            {
                SecurityGroupService securityGroupService = new SecurityGroupService();

                try
                {
                    if (Page.IsValid)
                    {
                        securityGroupService.DeleteSecurityGroup(Convert.ToInt64(hiddenFieldSecurityGroup.Value));
                        FlashMessage1.Message = "Successfully Deleted";
                        FlashMessage1.CssClass = "okmessage";
                        FlashMessage1.Display();
                    }
                }
                catch (Exception exception)
                {
                    FlashMessage1.Message = "Deleting Failed";
                    FlashMessage1.CssClass = "errormessage";
                    FlashMessage1.Display();
                }

                // LoadOrganizationLevelList(Convert.ToInt64(dropDownListOrganizationLevel.SelectedItem.Value));
                ClearInputs();
            }
        }


        protected void DisableComponants()
        {
            if (ViewState["ButtonBehaviour"] == ("Add"))
            {
                buttonModify.Enabled = false;

                buttonDelete.Enabled = false;

                textBoxSecurityGroupCode.Enabled = true;
                textBoxSecurityGroupCode.Text = String.Empty;
                textBoxDescription.Enabled = true;
                textBoxDescription.Text = string.Empty;
            }
            else if (ViewState["ButtonBehaviour"] == "Modify")
            {
                buttonAdd.Enabled = false;
                buttonDelete.Enabled = false;
                textBoxSecurityGroupCode.Enabled = true;
                textBoxDescription.Enabled = true;

            }
            else if (ViewState["ButtonBehaviour"] == "Delete")
            {

            }
            else if (ViewState["ButtonBehaviour"] == "Save")
            {

            }
            else if (ViewState["ButtonBehaviour"] == "Clear")
            {

            }

            else if (ViewState["ButtonBehaviour"] == "Load")
            {
                textBoxSecurityGroupCode.Enabled = false;
                textBoxSecurityGroupCode.Text = string.Empty;
                textBoxDescription.Enabled = false;
                textBoxDescription.Text = string.Empty;
                buttonAdd.Enabled = true;

                buttonModify.Enabled = false;
                buttonDelete.Enabled = false;
                buttonDelete.Enabled = false;

            }
            else if (ViewState["ButtonBehaviour"] == "GridSelectedChanged")
            {
                buttonAdd.Enabled = false;
                buttonModify.Enabled = true;
                buttonDelete.Enabled = true;
                textBoxSecurityGroupCode.Enabled = false;
                textBoxDescription.Enabled = false;
              
            }

        }
    }
}
